package org.dromara.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.shopping.domain.chat.ImUser;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.dromara.shopping.mapper.ImUserMapper;
import org.dromara.shopping.service.IImUserService;

import java.util.Date;

/**
 * 用户Service业务层处理
 *
 * @author Mr.Zhou
 * @date 2024-10-30
 */
@RequiredArgsConstructor
@Service
public class ImUserServiceImpl extends ServiceImpl<ImUserMapper, ImUser> implements IImUserService {

    private final ImUserMapper imUserMapper;

    private final PasswordEncoder passwordEncoder;

    @Override
    public ImUser getUserBySysUserId(LoginUser loginUser) {
        QueryWrapper<ImUser> qw = new QueryWrapper<>();
        qw.eq("sys_user_id", loginUser.getUserId());
        qw.last("limit 1");
        ImUser imUser = imUserMapper.selectOne(qw);
        if(imUser == null){
            imUser = new ImUser();
            imUser.setSysUserId(loginUser.getUserId());
            imUser.setUserName(loginUser.getUsername());
            imUser.setIsBanned(0);
            imUser.setSex(0);
            imUser.setLastLoginTime(new Date());
            imUser.setPassword(passwordEncoder.encode("123456"));
            imUser.setCreatedTime(new Date());
            imUserMapper.insert(imUser);
        }
        return imUser;
    }
}
